package test;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketExample {

    public static final int PROT = 10002;
    
    private static ServerSocket serverSocket;
    private static Socket socket;
    
    private static DataInputStream input;
    private static DataOutputStream output;
    
    @SuppressWarnings("unused")
    private static OutputStreamWriter outputStreamWriter;
    private static InputStreamReader inputStreamReader;
    
    
    private static char []bytes = new char[1024 * 3];
    
    public static void main(String[] args) throws IOException
    {
        SocketExample example = new SocketExample();
        example.initSocket();
        example.read();
    }
    
    private void initSocket() throws IOException
    {
        serverSocket = new ServerSocket(PROT);
        System.out.println("start: " + serverSocket);
        socket = serverSocket.accept();
        input = new DataInputStream(socket.getInputStream());
        output = new DataOutputStream(socket.getOutputStream());
        outputStreamWriter = new OutputStreamWriter(output, "GBK");
        inputStreamReader = new InputStreamReader(input,"GBK");
    }
    
    private void read() throws IOException
    {
        while(true)
        {
            try {
                try {
                    int cmd = input.readInt();
                    System.out.println("As3 send a CMD number: " + cmd);
                    double i = input.readDouble();
                    System.out.println(i);
                    int j = input.readInt();
                    System.out.println(j);
                    
                    int len = inputStreamReader.read(bytes, 0, 100);
                    
                    String string = "-->";
                    string += new String(bytes, 0, len);
                    System.out.println(string);
                    write(2, "Hello as3  你好啊啊\n你大爷的啊啊啊".getBytes("GBK"), cmd);
                    output.flush();
                    
                } catch (Exception e) {
                    // TODO: handle exception
                }
                finally
                {
    //                socket.close();
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
            finally
            {
    //            serverSocket.close();
            }
        }
    }
    
    private void write(int index, byte bytes[], int cmd) throws IOException
    {
        System.out.println("Send to client --> " + index);
        output.writeInt(index);
        output.writeInt(cmd);
        
//        output.flush();
        switch (index) {
        case 1:
            
            break;
        case 2:
            if(bytes != null)
            {
                output.writeInt(bytes.length);
                System.out.println("发送的数据包长度--> " + bytes.length);
                output.write(bytes);
//                output.flush();
            }
            break;
        default:
            break;
        }
        
        output.flush();
    }
    
    @SuppressWarnings("unused")
    private void closeSocket() throws IOException
    {
        serverSocket.close();
        socket.close();
    }
}